package com.example.commonlib.base;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;

/**
 * Created by yuyue on 15/12/10.
 */
public abstract class BaseFragment extends Fragment {


    private boolean injected = false;


    protected Activity mActivity;
    //切换用frgment相关
    protected FragmentManager fragmentManager;
    protected FragmentManager parentFragmentManager;
    protected int currentView = -1;
    protected Fragment fragment = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        injected = true;
        fragmentManager = getChildFragmentManager();
        parentFragmentManager = getFragmentManager();
        this.mActivity = getActivity();
        View view = createXmlView(inflater,container,savedInstanceState);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ButterKnife.bind(this,this.getView());
//        if (!injected) {
//            x.view().inject(this, this.getView());
//        }

        init(view, savedInstanceState);
        getData(view, savedInstanceState);
        setData(view,savedInstanceState);
        setLisitener(view, savedInstanceState);
    }

    /**
     * 创建布局view
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    protected abstract View createXmlView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);


    /**
     * 切换fragment
     * @param resid   容器id
     * @param fragmentResId  fragmengt id
     */
    protected void changeFragment(int resid,int fragmentResId){
        changeFragment(resid, fragmentResId, false, null, 0, 0);
    }

    /**
     * 切换fragment
     * @param resid   容器id
     * @param fragmentResId  fragmengt id
     */
    protected void changeFragment(int resid,int fragmentResId,int inA,int outA){
        changeFragment(resid, fragmentResId, false, null,inA, outA);
    }

    /** 切换fragment
    * @param resid   容器id
    * @param fragmentResId  fragmengt id
    */
    protected void changeFragment(int resid,int fragmentResId,boolean isAddBackStack,String name){
        changeFragment(resid, fragmentResId, isAddBackStack, name,0,0);
    }


    protected void changeFragment(int resid, int fragmentResId, boolean isAddBackStack, String name, int inA, int outA){
        FragmentTransaction transaction = fragmentManager.beginTransaction();

        Fragment fragment = getInstanceByIndex(fragmentResId);
        if (fragment == null){
            return;
        }
        if (inA != 0 && outA != 0)
            transaction.setCustomAnimations(inA,outA);
        transaction.replace(resid, fragment);
        transaction.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

        if (isAddBackStack)
            transaction.addToBackStack(name);

        transaction.commitAllowingStateLoss();

        currentView = fragmentResId;
    }


    protected void removeFragment(Fragment fragment){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.remove(fragment);
        transaction.commitAllowingStateLoss();
    }



    /**
     * 隐藏fragment
     * @param fragment
     */
    protected void hideFragment(Fragment fragment){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragment);
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏fragment
     * @param fragmentTag
     */
    protected void hideFragment(int fragmentTag){
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.hide(fragmentManager.findFragmentById(fragmentTag));
        transaction.commitAllowingStateLoss();
    }


    /**
     * 切换fragment
     *
     * @param id fragmentId
     * @return  fragment
     */
    protected Fragment getInstanceByIndex(int id){
        return null;
    }


    protected abstract void init(View view, Bundle savedInstanceState);

    protected void getData(View view, Bundle savedInstanceState){};

    protected void setData(View view, Bundle savedInstanceState){};

    protected void setLisitener(View view, Bundle savedInstanceState){};



    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
